/**
 * Copyright (c) 2018-2028, Chill Zhuang 庄骞 (smallchill@163.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.springblade.modules.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.common.constant.CommonConstant;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.modules.system.entity.Param;
import org.springblade.modules.system.mapper.ParamMapper;
import org.springblade.modules.system.service.IParamService;
import org.springblade.modules.system.vo.ParamVO;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 服务实现类
 *
 * @author Chill
 */
@Service
public class ParamServiceImpl extends BaseServiceImpl<ParamMapper, Param> implements IParamService {

	@Override
	public IPage<ParamVO> selectParamPage(IPage<ParamVO> page, ParamVO param) {
		return page.setRecords(baseMapper.selectParamPage(page, param));
	}


//初始化配置
	@Override
	public void initParam() {
		QueryWrapper<Param> paramQueryWrapper=new QueryWrapper<>();
		List<Param> params = baseMapper.selectList(paramQueryWrapper);
		for (Param param:params) {
			setParam(param);
		}

	}
	//保存端口配置
	@Override
	public void saveParam() {
		QueryWrapper<Param> paramQueryWrapper=new QueryWrapper<>();
		Param param = baseMapper.selectOne(paramQueryWrapper.lambda().eq(Param::getParamKey,"CLOUD_TCP_STARTPORT"));
		param.setParamValue(CommonConstant.CLOUD_TCP_STARTPORT.toString());
		baseMapper.updateById(param);
	}

	@Override
	public boolean submit(Param param) {
		setParam(param);
		return saveOrUpdate(param);
	}

	public void setParam(Param param){
		switch (param.getParamKey()){
			case "DEFAULT_REGISTER":
				CommonConstant.DEFAULT_REGISTER= "1".equals(param.getParamValue());
				break;
			case "DEFAULT_PASSWORD":
				CommonConstant.DEFAULT_PASSWORD=param.getParamValue();
				break;
			case "CLOUD_EMAIL":
				CommonConstant.CLOUD_EMAIL=param.getParamValue();
				break;
			case "CLOUD_TCP_STARTPORT":
				CommonConstant.CLOUD_TCP_STARTPORT= Integer.valueOf(param.getParamValue());
				break;
			case "CLOUD_PROXY_IP":
				CommonConstant.CLOUD_PROXY_IP=param.getParamValue();
				break;
			case "CLOUD_WEB_AUTO":
				CommonConstant.CLOUD_WEB_AUTO="1".equals(param.getParamValue());
				break;
			case "CLOUD_TCP_AUTO":
				CommonConstant.CLOUD_TCP_AUTO="1".equals(param.getParamValue());
				break;
			case "CLOUD_EXEC":
				CommonConstant.CLOUD_EXEC="1".equals(param.getParamValue());
				break;
			case "JMETER_FILE_PATH":
				CommonConstant.JMETER_FILE_PATH=param.getParamValue();
				break;
			case "CHFS_URL":
				CommonConstant.CHFS_URL=param.getParamValue();
				break;
			default:break;
		}
	}
}
